import z from "zod";

/** 消息代码模板 */
export const enum ENUM_MessageCodeTemplate {
  /** key不能为空 */
  REQUIRED = "%key%不能为空:40010",
  /** key格式不正确 */
  FORMATE = "%key%格式不正确:40020",
  /** key长度为val*/
  LENGTH = "%key%长度为%val%:40030",
  /** key最小长度为val */
  MIN_LENGTH = "%key%长度不能小于%val%:40040",
  /** key最大长度为val */
  MAX_LENGTH = "%key%长度不能大于%val%:40050",
  /** key最小值为val */
  MIN = "%key%最小值为%val%:40060",
  /** key最大值为val */
  MAX = "%key%最大值为%val%:40070",
  /** key解析失败 */
  PARSER = "%key%解析失败:40080",
  /** key验证失败 */
  AUTH = "%key%验证失败:40090",
  /** key不存在 */
  NOT_EXISTS = "%key%不存在:40100",
  /** key数据类型不正确 */
  TYPE = "%key%数据类型不正确:40110",
}

/** 获取代码错误信息 */
export const getMessageCode = (
  MessageCodeTemplate: ENUM_MessageCodeTemplate,
  key: string,
  value?: string
) => {
  if (value)
    return MessageCodeTemplate.replace("%key%", key).replace("%val%", value);
  else return MessageCodeTemplate.replace("%key%", key);
};

export const valid_base_string = (name: string, value: string = "1") => {
  return z
    .string({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(Number(value), {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN_LENGTH, name, value),
    });
};

/** 数值 - 填写、数值、最小值为0 */
export const valid_base_number = (name: string, value: string = "0") => {
  return z
    .number({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(0, getMessageCode(ENUM_MessageCodeTemplate.MIN, "0"));
};

/** 真假 - 填写、boolean */
export const valid_base_boolean = (name: string) => {
  return z.boolean({
    required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
    invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
  });
};

/** 日期时间 - 填写、Date */
export const valid_base_dateTime = (name: string) => {
  return z.date({
    required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
    invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
  });
};

/** 枚举类型参数 - 填写、枚举 */
export const valid_base_enum = (e: z.EnumLike, name: string) => {
  return z.nativeEnum(e, {
    required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
    invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
  });
};

/** 数组 - 填写、数组、默认空数组 */
export const valid_base_array = (arr: z.ZodTypeAny, name: string) => {
  return z.array(arr, {
    required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
    invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
  });
};

/** 名称 - 填写、字符串、不能为空 */
export const valid_public_name = valid_base_string("名称");

/** _id参数 - 填写、字符串、长度24 */
export const valid_public_id = (
  name: string = "id",
  minlength: string = "24"
) => {
  return valid_base_string(name, minlength);
};

/** 店铺Id参数 - 填写、字符串、默认为空 */
export const valid_public_shopId = valid_base_string("店铺Id", "0").default("");

/** 手机号参数 - 填写、字符串、长度11 */
export const valid_public_phone = (
  name: string = "手机号",
  value: string = "11"
) => {
  return z
    .string({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(1, {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN_LENGTH, name, value),
    });
};

/** 密码参数 - 填写、字符串、最小长度3 */
export const valid_public_password = (
  name: string = "密码",
  value: string = "3"
) => {
  return z
    .string({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(3, {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN_LENGTH, name, value),
    });
};

/** 比例参数 - 填写、数值、最小0、最大100 */
export const valid_public_ratio = (name: string) => {
  return z
    .number({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(0, {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN, name, "0"),
    })
    .max(100, {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN, name, "100"),
    });
};

/** 操作人 - 填写、字符串、不能为空 */
export const valid_public_operator = (name: string = "操作员") => {
  return z
    .string({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .min(1, {
      message: getMessageCode(ENUM_MessageCodeTemplate.MIN, name, "1"),
    });
};

/** 备注 - 填写、字符串、不能为空 */
export const valid_public_desc = (name: string = "备注") => {
  return z
    .string({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .default("");
};

/** 根据 _Id 删除 */
export const valid_public_delete = z.object({
  _id: valid_public_id(),
});

/** 创建日期 - 选填、日期、缺省值当天 */
export const valid_public_createdAt = (name: string = "创建日期") => {
  return z
    .date({
      required_error: getMessageCode(ENUM_MessageCodeTemplate.REQUIRED, name),
      invalid_type_error: getMessageCode(ENUM_MessageCodeTemplate.TYPE, name),
    })
    .default(new Date())
    .optional();
};
